package com.dx.wx.controller;

import com.alibaba.fastjson.JSONObject;
import com.dx.wx.service.DxOrderService;
import com.dx.wx.service.DxUserService;
import com.dx.wx.service.DxWxAppService;
import com.ejlchina.data.Mapper;
import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import com.yf.service.TokenService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/dx/wx/app")
public class ApiDxWxAppController {

    @Autowired
    private DxUserService dxUserService;

    @Autowired
    private DxWxAppService dxWxAppService;

    @PostMapping("/wx/login")
    @IgnoreAuth
    public R loginByWx(String code,String appId){
        return dxUserService.loginByWx(code,appId);
    }

    @PostMapping("/test/login")
    @IgnoreAuth
    public R loginByTest(String phone,String appId){
        return dxUserService.loginByTest(phone,appId);
    }

    @Resource
    private TokenService tokenService;

    @PostMapping("/phone/login")
    @IgnoreAuth
    public R loginByPhone(String code,String openId,String unionId,String role,String topPhone,String appId){
        if(role!=null&&role.equals("4")){
            dxUserService.saveUser(code,role,topPhone,openId,unionId);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("field_z79am__c",code);
            jsonObject.put("field_Rh3vp__c",role);
            return  R.ok(tokenService.createToken(Long.valueOf(code))).putExtra("user",jsonObject.toString());
        }
        return  dxUserService.loginByPhone(code,openId,unionId,appId);
    }

    /**
     * 根据用户的级别生成微信二维码
     * workType 工作类型 0 全职 1兼职
     * @param phone
     * @return
     */
    @PostMapping("/qrCode")
    public void createQrCode(@LoginUserId Long phone, HttpServletResponse response,int role,int workType,String appId){
       dxWxAppService.createQrCode(phone,role,workType,response,appId);
    }


    public void createCusQrCode(){

    }



    @PostMapping("/money")
    @IgnoreAuth
    public R money(@RequestBody Map map){
        dxWxAppService.sendMoney(map);
        return R.ok();
    }

    @Autowired
    private DxOrderService dxOrderService;

    @GetMapping("/money/test")
    @IgnoreAuth
    public R testMoney(String orderNo){
       Mapper mapper =  dxOrderService.findByOrderNo(orderNo);
       dxWxAppService.sendMoney(JSONObject.parseObject(mapper.toString()));
       return R.ok();
    }

}
